package com.mk.task;

import com.mk.constant.RedisCacheConstant;
import com.mk.entity.Article;
import com.mk.entity.Comment;
import com.mk.mapper.ArticleMapper;
import com.mk.mapper.CommentMapper;
import com.mk.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Component
@RequiredArgsConstructor
public class LikeTask {

    private final ArticleMapper articleMapper;
    private final CommentMapper commentMapper;
    private final RedisUtil redisUtil;
    private final HashMap<Long, Long> article_hm = new HashMap<>();
    private final HashMap<Long, Long> comment_hm = new HashMap<>();
    private final List<Article> articleList = new ArrayList<>();
    private final List<Comment> commentList = new ArrayList<>();

    //每两个小时执行一次
    @Scheduled(cron = "${task.likeScheduled.cron}")
    @Transactional
    public void updateLikeCount() {
        Map<Object, Object> article_map = redisUtil.getHashEntries(RedisCacheConstant.LIKE_ARTICLE_KEY);
        Set<Object> article_keySet = article_map.keySet();
        redisUtil.delete(RedisCacheConstant.LIKE_ARTICLE_KEY);
        if (!article_hm.isEmpty()) article_hm.clear();
        for (Object article_id : article_keySet) {
            Long articleId = Long.valueOf(article_id.toString().split("::")[0]);
            if (article_hm.containsKey(articleId)) {
                article_hm.put(articleId, article_hm.get(articleId) + 1L);
            } else {
                article_hm.put(articleId, 1L);
            }
        }
        if (!articleList.isEmpty()) articleList.clear();
        for (Long articleId : article_hm.keySet()) {
            Article article = articleMapper.selectById(articleId);
            article.setBeLiked(article.getBeLiked() + article_hm.get(articleId));
            articleList.add(article);
        }
        articleMapper.updateById(articleList);


        Map<Object, Object> comment_map = redisUtil.getHashEntries(RedisCacheConstant.LIKE_COMMENT_KEY);
        Set<Object> comment_keySet = comment_map.keySet();
        redisUtil.delete(RedisCacheConstant.LIKE_COMMENT_KEY);
        if (!comment_hm.isEmpty()) comment_hm.clear();
        for (Object comment_id : comment_keySet) {
            Long commentId = Long.valueOf(comment_id.toString().split("::")[0]);
            if (comment_hm.containsKey(commentId)) {
                comment_hm.put(commentId, comment_hm.get(commentId) + 1L);
            } else {
                comment_hm.put(commentId, 1L);
            }
        }
        if (!commentList.isEmpty()) commentList.clear();
        for (Long commentId : comment_hm.keySet()) {
            Comment comment = commentMapper.selectById(commentId);
            comment.setBeLiked(comment.getBeLiked() + comment_hm.get(commentId));
            commentList.add(comment);
        }
        commentMapper.updateById(commentList);
    }
}
